package leetcode.primary;

import utils.linkedList.LinkedList;
import utils.linkedList.ListNode;

public class T142 {
    public ListNode detectCycle(ListNode head) {
        if (head==null || head.next==null){
            return null;
        }
        ListNode low = head,fast = head;
        int lowIndex = 0;
        do {
            low = low.next;
            lowIndex++;
            if (fast.next!=null){
                fast = fast.next.next;
            }else {
                return null;
            }
            if (low.equals(fast)){
                return low;
            }
        }while (fast!=null);
        int count = 0;
        int startIndex ,endIndex;
        int length;
//        startIndex+lowIndex = length;
//        length - startIndex = count+1;
        ListNode node = low;
        while (node.next!=low){
            node = node.next;
            count++;
        }
        return null;
    }

    public static void main(String[] args) {
        T142 t142 = new T142();
        LinkedList list = new LinkedList();
        ListNode head = list.initLinkedList(new int[]{0, 1, 2, 3, 4, 5,6,7,8});
        list.getLast().next = list.inquireListNode(3);
        ListNode node = t142.detectCycle(head);
        System.out.println(node.val);
    }
}
